import { defineStore } from 'pinia';

export const useUserStore = defineStore('userinfo', {
  state: () => ({
    id: 0,
    openid: '',
    username: '',
    avatar: '',
    tdid: 0,
    inputtime: null,
    gptKey: '',
    code: '',

    region: '',
    secretId: '',
    secretKey: '',
    locationData: '',
    bucketList: '',
  }),
  actions: {
    updateUserInfo(userInfo) {
      const {
        id,
        openid,
        username,
        avatar,
        tdid,
        inputtime,
        gptKey,
        code,

        region,
        secretId,
        secretKey,
        locationData,
        bucketList,
      } = userInfo;

      console.log('actionsuserInfo2', userInfo);
      this.id = id;
      this.openid = openid;
      this.username = username;
      this.avatar = avatar;
      this.tdid = tdid;
      this.inputtime = inputtime;
      this.gptKey = gptKey;
      this.code = code;

      this.region = region;
      this.secretId = secretId;
      this.secretKey = secretKey;

      this.locationData = locationData;
      this.bucketList = bucketList;
    },
    loadUserInfo() {
      const storedUserInfo = JSON.parse(localStorage.getItem('user'));
      if (storedUserInfo) {
        this.updateUserInfo(storedUserInfo);
      }
    },
  },
  persist: {
    enabled: true,
    strategies: [
      {
        key: 'user',
        storage: localStorage, // 可以选择对应的存储形式（localStorage或者sessionStroage）
      },
    ],
  },
});
